`
https://leetcode.cn/problems/contains-duplicate-ii/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var containsNearbyDuplicate = function (nums, k) {
  if (nums.length === 0) return false

  const win = {}

  let left = 0, right = 0

  // 维护一个长度为 k 的滑动窗口
  while (right < nums.length) {
    const n = nums[right]
    right++

    win[n] = (win[n] || 0) + 1

    if (win[n] >= 2) return true

    // 当长度大于 k 时，缩小窗口
    if (right - left > k) {
      const m = nums[left]
      left++

      win[m] -= 1
    }
  }

  return false
};

console.log(containsNearbyDuplicate([1, 2, 3, 1], 3)) // true
console.log(containsNearbyDuplicate([1, 0, 1, 1], 1)) // true
console.log(containsNearbyDuplicate([1, 2, 3, 1, 2, 3], 2)) // false